import { createSSRApp } from "vue";
import App from "./App.vue";
import uviewPlus from "uview-plus";

import pinia from "./store/index";

import * as dayjs from "dayjs";
import "dayjs/locale/zh-cn";
dayjs.locale("zh-cn");

import { checkLogin } from "@/libs/login";
// import wechatMenuPlugin from "./plugin/wechatMenuPlugin";
export function createApp() {
  const app = createSSRApp(App);
  // // 使用微信菜单插件(除了首页其它页面隐藏分享)
  // app.config.compilerOptions.isCustomElement = (tag) => { return tag.startsWith('wx-open-subscribe') }
  app.use(pinia);
  app.use(uviewPlus);
  // 添加路由前置守卫
  uni.addInterceptor("navigateTo", {
    invoke(options) {
      // 判断目标页面是否需要登录权限
      const needAuthPages = [
        "/pages/tabbar/home",
        "/pages/tabbar/my",
        // 其他需要登录的页面
      ];

      if (needAuthPages.includes(options.url.split("?")[0]) && !checkLogin()) {
        // 重定向到登录页
        uni.redirectTo({
          url: "/pages/users/login",
        });
        return false; // 阻止原跳转
      }
      return options;
    },
  });
  return {
    app,
  };
}
